﻿$import( "SmitearWebGL.Objects.Mesh" );
$import( "SmitearWebGL.Core.Geometry.InputDataSet" );
$import( "SmitearWebGL.Core.Geometry.VertexDataSet" );

SmitearWebGL.Objects.Primitive.SkyBox = SmitearWebGL.Objects.Mesh.extend(
{
	/* * * * * * * * constructor * * * * * * * */

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.Primitive.SkyBox" );
	}
	,
	_build : function()
	{
		var indices = [
		0, 3, 1, 2, 1, 3, // Front face
		5, 6, 4, 7, 4, 6, // Back face
		4, 7, 0, 3, 0, 7, // Left face
		1, 2, 5, 6, 5, 2, // Right face
		3, 7, 2, 6, 2, 7, // Top face
		4, 0, 5, 1, 5, 0 // Bottom face
		];
			
		var vertices = [
			- 1.0, - 1.0,  1.0, 1.0, - 1.0,  1.0, 1.0,  1.0,  1.0, - 1.0,  1.0,  1.0, - 1.0, - 1.0,  - 1.0, 1.0, - 1.0,  - 1.0, 1.0,  1.0,  - 1.0, - 1.0,  1.0,  - 1.0
		];
		
		var uvs = [
			- 1.0, - 1.0,  1.0, 1.0, - 1.0,  1.0, 1.0,  1.0,  1.0, - 1.0,  1.0,  1.0, - 1.0, - 1.0,  - 1.0, 1.0, - 1.0,  - 1.0, 1.0,  1.0,  - 1.0, - 1.0,  1.0,  - 1.0
		];
		
		this._vertices = vertices;
		this._indices = indices;
			
		var inputDatas = [
			new SmitearWebGL.Core.Geometry.InputDataSet( indices, vertices, 3 ),
			new SmitearWebGL.Core.Geometry.InputDataSet( indices, uvs, 3 )
		];
		
		var vOutputData = SmitearWebGL.Core.Geometry.Geometry.buildRawData( inputDatas );
		
		var vAdj = SmitearWebGL.Core.Geometry.Geometry.buildVertexAdjancency( inputDatas[0] );
		var fNormals = SmitearWebGL.Core.Geometry.Geometry.buildFaceNormal( inputDatas[0] );
		var nOutpuData = SmitearWebGL.Core.Geometry.Geometry.buildVertexNormal( inputDatas[0], vAdj, fNormals, new SmitearWebGL.Core.Geometry.InputDataSet( vOutputData.indices, null, 3 ) );
		
		var rawData = SmitearWebGL.Core.Geometry.Geometry.combineVertexDatas( [
			new SmitearWebGL.Core.Geometry.VertexDataSet( vOutputData.data, 6 ),
			new SmitearWebGL.Core.Geometry.VertexDataSet( nOutpuData.data, 3 ),
		] );
		
		this._boundingBox = SmitearWebGL.Core.Geometry.Geometry.computeBoundingBoxByRawVertices( vertices );
		
		var vertDescs = [
			new SmitearWebGL.Resources.VertexElementDescription( this.context.gl, 3, 36, 0, SmitearWebGL.Resources.Buffer.DataType.FLOAT, SmitearWebGL.Resources.Buffer.AttribLocation.POSITION ),
			new SmitearWebGL.Resources.VertexElementDescription( this.context.gl, 3, 36, 12, SmitearWebGL.Resources.Buffer.DataType.FLOAT, SmitearWebGL.Resources.Buffer.AttribLocation.TEXCOORD ),
			new SmitearWebGL.Resources.VertexElementDescription( this.context.gl, 3, 36, 24, SmitearWebGL.Resources.Buffer.DataType.FLOAT, SmitearWebGL.Resources.Buffer.AttribLocation.NORMAL )
		]
		
		var idxDescs = [
			new SmitearWebGL.Resources.IndexElementDescription( this.context.gl, vOutputData.indices.length, 0, SmitearWebGL.Resources.Buffer.DataType.UNSIGNED_SHORT, SmitearWebGL.Resources.IndexBuffer.DrawType.TRIANGLES )
		]
		
		this.createVertexBuffer( rawData, vertDescs );
		this.createIndexBuffer( vOutputData.indices, idxDescs );
	}
	,
	_createResource : function( params )
	{
		this._super();
		
		this._build();
	}
}
);